package com.huangyi.rabbitmq_spring.controller;

import com.huangyi.rabbitmq_spring.constants.Constants;
import com.huangyi.rabbitmq_spring.model.UserInfo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/producer")
public class ProducerController {
    @Autowired
    private RabbitTemplate rabbitTemplate;


    @RequestMapping("/work")
    public String work(){
        for (int i = 0; i <10 ; i++) {
            rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE,"hello work queue" + i);
        }
        return "发送成功";
    }
    @RequestMapping("/fanout")
    public String fanout(){
        for (int i = 0; i <10 ; i++) {
            rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE,"","hello fanout queue" + i);
        }
        return "发送成功";
    }
    @RequestMapping("/direct/{routingKey}")
    public String direct(@PathVariable("routingKey") String routingKey){
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE,routingKey,"hello direct this is routingKey " + routingKey);
        return "发送成功";
    }
    @RequestMapping("/topic/{routingKey}")
    public String topic(@PathVariable("routingKey") String routingKey){
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE,routingKey,"hello topic " + routingKey);
        return "发送成功";
    }
    @RequestMapping("/object")
    public String sendMessageObject(){
        UserInfo userInfo = new UserInfo();
        userInfo.setId(1);
        userInfo.setPassword("123456");
        userInfo.setUsername("zhangsan");
        rabbitTemplate.convertAndSend("",Constants.WORK_QUEUE,userInfo);
        return "发送成功";
    }
//    public void sendObjectMessage(UserInfo userInfo){
//        userInfo.setId(1);
//        userInfo.setPassword("123456");
//        userInfo.setUsername("zhangsan");
//        rabbitTemplate.convertAndSend(Constants.WORK_QUEUE,userInfo);
//    }
}
